package com.mydomain;

import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.Motor;
import lejos.nxt.NXTRegulatedMotor;
import lejos.nxt.SensorPort;

public class LineTester {
	
	private static boolean isRun = true;
	private static boolean notWantExit = true;
	private static int ROT_ANGLE = 60;
	private static NXTRegulatedMotor A = Motor.A;
	private static NXTRegulatedMotor B = Motor.B;
	
	
	public static void main(String[] args) {
		
		Button.ENTER.addButtonListener(new ButtonListener() {	
			@Override
			public void buttonReleased(Button b) {
				isRun = false;
				notWantExit = false;
			}
			@Override
			public void buttonPressed(Button b) {}
		});
		Button.ESCAPE.addButtonListener(new ButtonListener() {	
			@Override
			public void buttonReleased(Button b) {
				isRun = true;
			}
			@Override
			public void buttonPressed(Button b) {}
		});
		
		
		
		Thread appThread = new Thread() {
			@Override
			public void run() {
				A.setSpeed(50);
				B.setSpeed(50);
				while (notWantExit) {
					while (isRun) {
						A.rotate(ROT_ANGLE, true);
						B.rotate(ROT_ANGLE, true);
						A.waitComplete();
						B.waitComplete();
					}
				}
				System.exit(0);
			}
		};
		appThread.start();
		
		Thread lightThread = new Thread() {
			@Override
			public void run() {
				LightSensor light = new LightSensor(SensorPort.S2);
				
				while (true) {
					LCD.drawInt(light.getLightValue(), 4, 0, 0);
					LCD.drawInt(light.getNormalizedLightValue(), 4, 0, 1);
					LCD.drawInt(SensorPort.S2.readRawValue(), 4, 0, 2);
					LCD.drawInt(SensorPort.S2.readValue(), 4, 0, 3);
					if (light.getNormalizedLightValue() > 600) {
						isRun = false;
					}
					if (light.getNormalizedLightValue() < 250) {
						isRun = true;
					}
			    }
			}
		};
		lightThread.start();
		
	}

}
